package com.home.qh.dao.hibernate;

import java.util.Calendar;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.home.qh.dao.BookingDao;
import com.home.qh.dao.CityDao;
import com.home.qh.model.AdditionalClause;
import com.home.qh.model.Booking;
import com.home.qh.model.City;
import com.home.qh.model.Country;
import com.home.qh.model.TransFreight;
import com.home.qh.model.TransGoodsDesc;
import com.home.qh.model.TransParty;


/**
 * This class interacts with Spring's HibernateTemplate to save/delete and
 * retrieve Role objects.
 *
 * @author <a href="mailto:bwnoll@gmail.com">Bryan Noll</a> 
 */
@Repository
public class BookingDaoHibernate extends GenericDaoHibernate<Booking, Long> implements BookingDao {

    /**
     * Constructor to create a Generics-based version using City as the entity
     */
    public BookingDaoHibernate() {
        super(Booking.class);
    }

    /**
     * {@inheritDoc}
     */
    public List<Booking> getBookingByBooking(Booking booking, String sort, boolean desc, int startResult, int maxResults) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Booking.class);
        System.out.println("get Booking by Booking from BookingDaoHibernate.java");
//        if (city.getCode() != null) {
//        	criteria.add(Restrictions.or(Restrictions.eq("code", city.getCode()), Restrictions.eq("name", city.getName())));
//		}
//        
//        if (city.getCountry() != null) {        	
//        	criteria.add(Restrictions.eq("country.id", city.getCountry().getId()));
//		}
        
        if (booking.getShipper() != null){
        	criteria.add(Restrictions.eq("shipper.id", booking.getShipper().getId()));
        }
        
        if (booking.getConsignee() != null){
        	criteria.add(Restrictions.eq("consignee.id", booking.getConsignee().getId()));
        }
        
        if (booking.getVoyage() != null){
        	criteria.add(Restrictions.eq("voyage.id", booking.getVoyage().getId()));
        }
        
        if (booking.getPartner() != null){
        	criteria.add(Restrictions.eq("partner.id", booking.getPartner().getId()));
        }

        if(sort != null && !"".equalsIgnoreCase(sort)) {
            if(desc) criteria.addOrder(Order.desc(sort));
            else criteria.addOrder(Order.asc(sort));
        }

        criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
        
        return getHibernateTemplate().findByCriteria(criteria, startResult, maxResults);
	}

	public List<TransParty> getTransPartyByBooking(long id) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TransParty.class);
		criteria.add(Restrictions.eq("booking.id", id));
		return getHibernateTemplate().findByCriteria(criteria);
	}

	public List<AdditionalClause> getAdditionalClauseByBooking(long bookingId) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(AdditionalClause.class);
		criteria.add(Restrictions.eq("booking.id", bookingId));
		return getHibernateTemplate().findByCriteria(criteria);
	}

	public List<TransFreight> getTransFreightByBooking(long bookingId) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TransFreight.class);
		criteria.add(Restrictions.eq("booking.id", bookingId));
		return getHibernateTemplate().findByCriteria(criteria);
	}

	public List<TransGoodsDesc> getTransGoodsDescByBooking(long bookingId) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TransGoodsDesc.class);
		criteria.add(Restrictions.eq("booking.id", bookingId));
		return getHibernateTemplate().findByCriteria(criteria);
	}

	public int getOrderNumber() {
		// TODO Auto-generated method stub
		Calendar cal = Calendar.getInstance();
		DetachedCriteria criteria  = DetachedCriteria.forClass(Booking.class);
		int lastDate = cal.getActualMinimum(Calendar.DATE);
		cal.set(Calendar.DATE, lastDate);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		System.out.println("------"+cal.getTime());
		
		Calendar cal2 = Calendar.getInstance();
		int firstDate = cal2.getActualMaximum(Calendar.DATE);
		cal2.set(Calendar.DATE, firstDate);
		cal2.set(Calendar.HOUR_OF_DAY, 23);
		cal2.set(Calendar.MINUTE, 59);
		cal2.set(Calendar.SECOND, 59);
		cal2.set(Calendar.MILLISECOND, 999);
		System.out.println("------"+cal2.getTime());
		
		criteria.add(Restrictions.between("bookingDate", cal.getTime(), cal2.getTime()));
		System.out.println("======"+getHibernateTemplate().findByCriteria(criteria).size());
//		criteria.add(criterion)
		return 0;
	}
}
